package com.burgeon.backend.SHOP.rest;
/**
 * 根据账号查找退款单 发货同步
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.Invoices;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetRefundByAccount extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetRefundByAccount(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<Invoices> getData() {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        ArrayList<Invoices> list = new ArrayList<Invoices>();
        String str = null;
        try {
            str = RESULT(Data(), url);


            String code = new JSONArray(str).getJSONObject(0).getString("code");
            if (!"0".equals(code)) {
                throw new Exception();
            }

            JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

            for (int i = 0; i < jsonArray.length(); i++) {
                Invoices invoices = new Invoices();

                invoices.setID(jsonArray.getJSONArray(i).getString(0));
                invoices.setDOCNO(jsonArray.getJSONArray(i).getString(1));
                invoices.setBILLDATE(jsonArray.getJSONArray(i).getString(2));
                invoices.setXSNO(jsonArray.getJSONArray(i).getString(3));
                invoices.setXSRETNO(jsonArray.getJSONArray(i).getString(4));
                invoices.setRET_DESCRIPTION(jsonArray.getJSONArray(i).getString(5));
                invoices.setC_VIP_ID(jsonArray.getJSONArray(i).getString(6));
                invoices.setYC_RET_STATUS(jsonArray.getJSONArray(i).getString(7));
                invoices.setIS_UP(jsonArray.getJSONArray(i).getString(8));
                invoices.setDESCRIPTION(jsonArray.getJSONArray(i).getString(9));
               // invoices.setREJECT_DESCRIPTION(jsonArray.getJSONArray(i).getString(10));
                invoices.setRET_DESCRIPTION(jsonArray.getJSONArray(i).getString(10));
                invoices.setC_STORE_NAME(jsonArray.getJSONArray(i).getString(11));
                invoices.setADDRESS(jsonArray.getJSONArray(i).getString(12));
                invoices.setPHONE(jsonArray.getJSONArray(i).getString(13));
                invoices.setCONTACTOR(jsonArray.getJSONArray(i).getString(14));
                invoices.setTOT_AMT_ACTUALSJ(jsonArray.getJSONArray(i).getString(15));
                invoices.setIS_UPRET(jsonArray.getJSONArray(i).getString(16));
                invoices.setBACK_DESCRIPTION(jsonArray.getJSONArray(i).getString(17));
                list.add(invoices);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("DOCNO");//单据编号
        columns.add("BILLDATE");//单据日期
        columns.add("XSNO");//线上单号
        columns.add("XSRETNO");//线上退款单号
        columns.add("RET_DESCRIPTION");//退货原因
        columns.add("C_VIP_ID");//购买顾客
        columns.add("YC_RET_STATUS");//退货状态
        columns.add("IS_UP");//同步状态
        columns.add("DESCRIPTION");//原单备注
        //columns.add("REJECT_DESCRIPTION");//驳回原因
        columns.add("RET_DESCRIPTION");//退货原因
        columns.add("'C_STORE_ID;NAME'");//收货店仓
        columns.add("ADDRESS");//收货地址
        columns.add("PHONE");//收货电话
        columns.add("CONTACTOR");//收货人
        columns.add("TOT_AMT_ACTUALSJ");//实际退款金额
        columns.add("IS_UPRET");//同步状态 v5
        columns.add("BACK_DESCRIPTION");//驳回原因

        params.put("columns", columns);
        params.put("table", "YC_RET_RETAIL");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();
        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine", "and");
        expr1v1.put("column", "YC_RET_STATUS");
        expr1v1.put("condition", "\" IN (1,2,3,4,5)\"");
        expr1.put("expr1", expr1v1);
//        expr1v2.put("column", "'IS_UP'");
//        expr1v2.put("condition", "\" IN (1,2)\"");
        expr1v2.put("column", "''");
        expr1v2.put("condition", "''");
        expr1.put("expr2", expr1v2);

//        expr2.put("column", "ISACTIVE");
//        expr2.put("condition", "Y");
        expr2.put("column", "''");
        expr2.put("condition", "''");

        paramss.put("combine", "and");//关系

        //时间倒序前500条
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetRefundByAccount g = new GetRefundByAccount("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
